package qnb.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import qnb.dao.ClassroomMapper;
import qnb.entity.Classroom;
import qnb.service.ClassroomService;

@Service
public class ClassroomServiceImpl implements ClassroomService {

	@Autowired
	private ClassroomMapper classroomMapper;

	@Override
	public List<Classroom> selectAllClassroom(String where, String order, int start, int rows) {
		return classroomMapper.selectAllClassroom(where,order,start,rows);
	}

	@Override
	public List<Classroom> selectAllClassroomByName() {
		return classroomMapper.selectAllClassroomByName();
	}

	@Override
	public Integer selectCountByClassroom(String where) {
		return classroomMapper.selectCountByClassroom(where);
	}

	@Override
	public Classroom selectClassroomById(Integer classroomId) {
		return classroomMapper.selectClassroomById(classroomId);
	}

	@Override
	public Integer insertClassroom(Classroom classroom) {
		if(2 == repetition(classroom.getClassroomName())){
			return 2;
		}
		return classroomMapper.insertClassroom(classroom);
	}

	@Override
	public Integer deleteClassroom(Integer Classroom_id) {
		
		return classroomMapper.deleteClassroom(Classroom_id);
	}

	@Override
	public Integer updateClassroom(Classroom classroom) {
		if(2 == repetition(classroom.getClassroomName())){
			return 2;
		}
		return classroomMapper.updateClassroom(classroom);
	}

	public Integer repetition(String name){
		List<Classroom> list = classroomMapper.selectAllClassroomByName();
		for (int i = 0; i <list.size() ; i++) {
			if (name.equals(list.get(i).getClassroomName())){
				return 2;
			}
		}
		return 0;
	}
}
